<template>
<el-card>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-row class="rowSearch">
        <el-col>
            <!-- @clear -->
            <el-input placeholder="请输入内容" v-model="query" class="inputSearch" @clear="clearSearch" clearable>
                <el-button slot="append" icon="el-icon-search" @click.prevent="handleSearch"></el-button>
            </el-input>
            <el-button type="success">添加用户</el-button>
        </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table :data="userlist" style="width: 100%" class="userTable">
        <!-- type="index" -->
        <el-table-column type="index" label="#" width="60">
        </el-table-column>
        <el-table-column prop="username" label="用户名">
        </el-table-column>
        <el-table-column prop="email" label="邮箱">
        </el-table-column>
        <el-table-column prop="phone" label="电话">
        </el-table-column>
        <!-- 时间过滤器 -->
        <el-table-column label="创建时间">
            <template slot-scope="userlist">{{userlist.row.create_time | fmtdate}}</template>
        </el-table-column>
        <el-table-column label="用户状态">
            <template slot-scope="scope">
                <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
            </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
            <template>
                <el-button size="mini" plain type="primary" icon="el-icon-edit" circle></el-button>
                <el-button size="mini" plain type="success" icon="el-icon-check" circle></el-button>
                <el-button size="mini" plain type="danger" icon="el-icon-delete" circle></el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination class="pages" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
</el-card>
</template>

<script>
export default {
    data() {
        return {
            query: '',
            userlist: [],
            currentPage: 1,
            pageSize: 5, // pagenum=1 pageize=2 意思是拿到第一条数据开始的2条数据
            total: 0
        }
    },
    created() {
        this.getUserList()
    },
    methods: {
        async getUserList() {
            // 需要授权的API需要增加Token认证
            // 除了登录之外的所有请求都需要授权；Authorization
            // const AUTH_TOKEN = sessionStorage.getItem('token')
            // this.$http.defaults.headers.common['Authorization'] = AUTH_TOKEN
            const res = await this.$http.post(`getUserList?query=${this.query}&currentPage=${this.currentPage}&pageSize=${this.pageSize}`)
            
            if(res.status === 200){
                const {data,msg,code} = res.data
                this.userlist = data.data
                this.total = data.total
                this.$message.success(msg)

            }else{
                this.$message.error('数据请求失败')
            }
        },
        // 分页
        handleSizeChange(val) { // 每页显示条数变换时触发
            // console.log(`每页 ${val} 条`);
            this.pageSize = val
            this.currentPage = 1
            this.getUserList()
        },
        // currentPage pagesize -> 
        // select * from table limit (currentPage-1)*pagesize,pagesize
        handleCurrentChange(val) { //当前页面改变时触发
            // console.log(`当前页: ${val}`);
            this.currentPage = val
            this.getUserList()
        },
        handleSearch(){
            this.getUserList()
        },
        clearSearch(){
            this.query = ''
            this.getUserList()
        }
    }
}
</script>

<style>
.inputSearch {
    width: 30%;
}

.rowSearch {
    margin-top: 20px;
}

.userTable {
    margin-top: 20px;
}

.pages {
    margin-top: 10px;
}
</style>
